package cn.gmcx.lazy.izze.controller;


import cn.gmcx.lazy.izze.enums.ConcernEnum;
import cn.gmcx.lazy.izze.exception.BizException;
import cn.gmcx.lazy.izze.pojo.Fans;
import cn.gmcx.lazy.izze.pojo.User;
import cn.gmcx.lazy.izze.service.IFansService;
import cn.gmcx.lazy.izze.service.IToDoService;
import cn.gmcx.lazy.izze.service.IUserService;
import cn.gmcx.lazy.izze.utils.Result;
import cn.gmcx.lazy.izze.utils.ToDoMessage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author GMCX
 * @since 2024-07-02
 */
@RestController
@RequestMapping("/fans")
public class FansController {
    @Autowired
    private IFansService service;
    @Autowired
    private IToDoService toDoService;

    @PostMapping("/add_fans")
    public Result fansAdd(@RequestParam(name = "masterId") Integer master_id, @RequestParam(name = "followId") Integer follow_id) {
        QueryWrapper<Fans> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("follower_id", master_id);
        queryWrapper.eq("followee_id", follow_id);
        Fans fans = new Fans();
        fans.setFollowerId(master_id);
        fans.setFolloweeId(follow_id);
        long count = service.count(queryWrapper);
//        if(count==0){
//           save = service.save(fans);
//           //更新粉丝数
//           service.updateFansNum(master_id,follow_id);
//        }
        if (count == 0) {
            service.saveFans(fans);
            toDoService.addToDo(master_id, ToDoMessage.FRIEND_APPLICATION, follow_id, 1);
        } else {
            return new Result(405, "您已经添加过该用户,请耐心等待处理");
        }
        return Result.success("关注成功，已向对方发送好友请求");
    }

    @GetMapping("/friend_select")
    public Result selectFriend(@RequestParam(name = "id") Integer masterId) {
        List<User> userList = service.getFriendList(masterId);
        return Result.success(userList);
    }

    @GetMapping("/friend_isConcern")
    public Result isConcern(@RequestParam(name = "friendId") Integer friendId, @RequestParam(name = "myId") Integer myId) {
        ConcernEnum status =  service.isConcern(friendId,myId);
//        Map<String,Object> map = new HashMap<>();
//        map.put("msg",status.getMessage());
//        map.put("status",status.getStatus());
        return Result.success(status);
    }

}
